package com.caigou.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
public class RabbitConfig {
    // MQ消息监听配置--默认
    public static final String DEFAULT_EXCHANGE = "direct.cms.exchange";
    // MQ消息监听配置--路由
    public static final String DEFAULT_ROUTING = "direct.queue.cms.buildHtmlImage";
    public static final String SYNC_ROUTER = "direct.queue.cms.syncData";

    //修改作者信息的消息队列
    public static final String DIRECT_QUEUE_CMS_ARTICLEAUTHOR = "direct.queue.cms.articleAuthor";

    // 生成文章静态页消息(分站)
    public static final String DIRECT_QUEUE_CMS_BUILD_ARTICLE = "direct.queue.cms.buildArticle";
    // 生成频道页消息(分站)
    public static final String DIRECT_QUEUE_CMS_BUILD_PAGE = "direct.queue.cms.buildPage";

    // 发送爬虫修改消息(分站测试环境)
    public static final String DIRECT_QUEUE_CMS_COLLECTION_CHANGE = "direct.queue.cms.collectionChange250";


    public static final String DIRECT_QUEUE_CMS_COLLECTION_CHANGE1 = "direct.queue.cms.collectionChange165";
    public static final String DIRECT_QUEUE_CMS_COLLECTION_CHANGE2 = "direct.queue.cms.collectionChange155";
    public static final String DIRECT_QUEUE_CMS_COLLECTION_CHANGE3 = "direct.queue.cms.collectionChange226";
    public static final String DIRECT_QUEUE_CMS_COLLECTION_CHANGE4 = "direct.queue.cms.collectionChange233";
    public static final String DIRECT_QUEUE_CMS_COLLECTION_CHANGE5 = "direct.queue.cms.collectionChange30";

    // 智能表单增加统计值的消息
    public static final String FORM_SAVE_STATISTICS = "direct.queue.cms.formSaveStatistics";

    // 消息转换器
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    // direct -- 交换机
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DEFAULT_EXCHANGE);
    }

    // 生成分享图静态页
    @Bean
    public Queue directQueue() {
        return new Queue(DEFAULT_ROUTING);
    }
    @Bean
    public Binding directBindingDefaultRouter() {
        return BindingBuilder.bind(this.directQueue()).to(this.directExchange()).with("cms.buildHtmlImage");
    }

    // 生成文章作者静态页
    @Bean
    public Queue directQueueCmsArticleAuthor() {
        return new Queue(DIRECT_QUEUE_CMS_ARTICLEAUTHOR);
    }
    @Bean
    public Binding directBindingCmsArticleAuthor() {
        return BindingBuilder.bind(this.directQueueCmsArticleAuthor()).to(this.directExchange()).with("cms.articleAuthor");
    }

    // 生成文章静态页(分站)
    @Bean
    public Queue directQueueCmsBuildArticle(){
        return new Queue(DIRECT_QUEUE_CMS_BUILD_ARTICLE);
    }
    @Bean
    public Binding directBindingCmsBuildArticle(){
        return BindingBuilder.bind(this.directQueueCmsBuildArticle()).to(this.directExchange()).with("cms.buildArticle");
    }

    // 生成频道页(分站)
    @Bean
    public Queue directQueueCmsBuildPage(){
        return new Queue(DIRECT_QUEUE_CMS_BUILD_PAGE);
    }
    @Bean
    public Binding directBindingCmsBuildPage(){
        return BindingBuilder.bind(this.directQueueCmsBuildPage()).to(this.directExchange()).with("cms.buildPage");
    }

    // 发送爬虫修改消息(分站)[测试环境分站]
    @Bean
    public Queue directQueueCmsCollectionChange(){
        return new Queue(DIRECT_QUEUE_CMS_COLLECTION_CHANGE);
    }
    @Bean
    public Binding directBindingCmsCollectionChange(){
        return BindingBuilder.bind(this.directQueueCmsCollectionChange()).to(this.directExchange()).with("cms.collectionChange250");
    }

    // 发送爬虫修改消息(分站)[测试环境分站]
    @Bean
    public Queue directQueueCmsCollectionChange1(){
        return new Queue(DIRECT_QUEUE_CMS_COLLECTION_CHANGE1);
    }
    @Bean
    public Binding directBindingCmsCollectionChange1(){
        return BindingBuilder.bind(this.directQueueCmsCollectionChange1()).to(this.directExchange()).with("cms.collectionChange165");
    }

    // 发送爬虫修改消息(分站)[测试环境分站]
    @Bean
    public Queue directQueueCmsCollectionChange2(){
        return new Queue(DIRECT_QUEUE_CMS_COLLECTION_CHANGE2);
    }
    @Bean
    public Binding directBindingCmsCollectionChange2(){
        return BindingBuilder.bind(this.directQueueCmsCollectionChange2()).to(this.directExchange()).with("cms.collectionChange155");
    }

    // 发送爬虫修改消息(分站)[测试环境分站]
    @Bean
    public Queue directQueueCmsCollectionChange3(){
        return new Queue(DIRECT_QUEUE_CMS_COLLECTION_CHANGE3);
    }
    @Bean
    public Binding directBindingCmsCollectionChange3(){
        return BindingBuilder.bind(this.directQueueCmsCollectionChange3()).to(this.directExchange()).with("cms.collectionChange226");
    }

    // 发送爬虫修改消息(分站)[测试环境分站]
    @Bean
    public Queue directQueueCmsCollectionChange4(){
        return new Queue(DIRECT_QUEUE_CMS_COLLECTION_CHANGE4);
    }
    @Bean
    public Binding directBindingCmsCollectionChange4(){
        return BindingBuilder.bind(this.directQueueCmsCollectionChange4()).to(this.directExchange()).with("cms.collectionChange233");
    }

    // 发送爬虫修改消息(分站)[测试环境分站]
    @Bean
    public Queue directQueueCmsCollectionChange5(){
        return new Queue(DIRECT_QUEUE_CMS_COLLECTION_CHANGE5);
    }
    @Bean
    public Binding directBindingCmsCollectionChange5(){
        return BindingBuilder.bind(this.directQueueCmsCollectionChange5()).to(this.directExchange()).with("cms.collectionChange30");
    }

    // 智能表单保存统计数据
    @Bean
    public Queue directQueueFormSaveStatistics(){ return new Queue(FORM_SAVE_STATISTICS); }
    @Bean
    public Binding directBindingFormSaveStatistics(){
        return BindingBuilder.bind(this.directQueueFormSaveStatistics()).to(this.directExchange()).with("cms.formSaveStatistics");
    }
}
